//
//  YZXOriginaView.swift
//  YZXWeibo
//
//  Created by 叶周翔 on 16/7/23.
//  Copyright © 2016年 叶周翔. All rights reserved.
//

import UIKit

class YZXOriginaView: UIView {
    
    var status: YZXStatus?{
        didSet{
            
            if let profileImage = status?.user?.profile_image_url{
                let url = NSURL(string:profileImage)
                HMAsyncDrawImage.sharedAsyncDrawImage.hm_AsyncDrawImage(url!.absoluteString, drawFinish: { (image) -> () in
                    self.iconView.image = image
                })
            }
            nameLabel.text = status?.user?.screen_name
            
            // 时间 根据服务器传回来的时间 调整时间 在每次给这个label赋值的时候调用NSDate的方法
            timeLabel.text = NSDate.date(status?.created_at ?? "未知时间")?.sinaDateDes()
            
            
            
            
            
            // 来源
            sourceLabel.text = status?.source
            
            // 会员等级
            memberImageView.image = status?.user?.mbrankImage
            
            
//            if let mbrank = status?.user?.mbrank {
//                if mbrank > 0 && mbrank <= 6 {
//                    let image = UIImage(named: "common_icon_membership_level\(mbrank)")
//                    memberImageView.image = image
//                } else {
//                    memberImageView.image = nil
//                }
//            }
            
            // 认证类型
            verifiedImageView.image = status?.user?.verifiedImage


//            if let verified_type = status?.user?.verified_type {
//                var image: UIImage? = nil
//                
//                switch verified_type {
//                case 0:
//                    // 认证用户
//                    image = UIImage(named: "avatar_vip")
//                case 2,3,5:
//                    // 企业认证:2,3,5
//                    image = UIImage(named: "avatar_enterprise_vip")
//                case 220:
//                    // 达人
//                    image = UIImage(named: "avatar_grassroot")
//                default:
//                    image = nil
//                }
//                
//                verifiedImageView.image = image
//            }
            
            // 微博内容
            contentLabel.text = status?.text
            
            // 将原创视图的URL赋值给微博的配图
            pictureView.imageURLs = status?.storePictureURLs
            
            // 更新约束
            pictureView.snp_updateConstraints { (make) -> Void in
                
                var marfin:CGFloat = 0
                if status?.storePictureURLs != nil && status?.storePictureURLs?.count > 0{
                    marfin = CellMargin
                }
                make.top.equalTo(contentLabel.snp_bottom).offset(marfin)
            }
        }

      
    }




    // MARK:初始化控件
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        setupUI()
    }
    
    /// 设置UI
    private func setupUI() {
        // 1.添加子控件
        self.addSubview(iconView)
        self.addSubview(nameLabel)
        self.addSubview(timeLabel)
        self.addSubview(sourceLabel)
        self.addSubview(memberImageView)
        self.addSubview(verifiedImageView)
        self.addSubview(contentLabel)
        self.addSubview(pictureView)
        // 2.添加约束
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(CellMargin)
            make.size.equalTo(CGSize(width: CellIconWH, height: CellIconWH))
        }
        
        // 名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CellMargin)
            make.top.equalTo(iconView)
        }
        
        // 时间label
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(iconView)
        }
        
        // 来源label
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        // 会员等级
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        // 认证图标
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        // 微博文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(12)
        }
        
        // 图片的内容
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
            
            // 不能设置右边的距离 它的宽度是要根据图片的张数不同随时更改的
            make.leading.equalTo(self).offset(CellMargin)
            make.size.equalTo(CGSize(width: 359, height: 359))
        }
        
        
        // 约束原创view的底部和 微博文本label底部重合
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(CellMargin)
        }
    }
    
    // MARK: - 懒加载
    /// 头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 用户名称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
    
    /// 时间label
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor())
    
    /// 来源label
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor())
    
    /// 会员等级
    private lazy var memberImageView: UIImageView = UIImageView(frame: CGRectZero)
    
    /// 认证图标
    private lazy var verifiedImageView: UIImageView = UIImageView(frame: CGRectZero)
    
    /// 微博文本内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: contentFont)
        
        // 设置多行
        label.numberOfLines = 0
        
        // 文字换行宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.size.width - 2 * CellMargin
        
        return label
    }()
    /// 微博配图视图
    lazy var pictureView:YZXPictureView = YZXPictureView()
}
